﻿using System.Security.Permissions;
using System.Windows.Forms;
using dasFX.VSTime.API;
using System;
using System.Linq;
using System.Collections.Generic;
using System.Diagnostics;

namespace dasFX.VSTime
{
    /// <summary>
    /// Summary description for MyControl.
    /// </summary>
    public partial class VSTimeControl : UserControl
    {
        private int _solutionId = 0;

        public VSTimeControl()
        {
            InitializeComponent();
        }

        /// <summary> 
        /// Let this control process the mnemonics.
        /// </summary>
        [UIPermission(SecurityAction.LinkDemand, Window = UIPermissionWindow.AllWindows)]
        protected override bool ProcessDialogChar(char charCode)
        {
              // If we're the top-level form or control, we need to do the mnemonic handling
              if (charCode != ' ' && ProcessMnemonic(charCode))
              {
                    return true;
              }
              return base.ProcessDialogChar(charCode);
        }

        /// <summary>
        /// Enable the IME status handling for this control.
        /// </summary>
        protected override bool CanEnableIme
        {
            get
            {
                return true;
            }
        }

        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Globalization", "CA1300:SpecifyMessageBoxOptions")]
        private void button1_Click(object sender, System.EventArgs e)
        {
            MessageBox.Show(this,
                            string.Format(System.Globalization.CultureInfo.CurrentUICulture, "We are inside {0}.button1_Click()", this.ToString()),
                            "VSTime");
        }

        private void VSTimeControl_Load(object sender, System.EventArgs e)
        {
            //BindData();

        }

        private void BindData(int SolutionID)
        {
            try
            {
                EventSummaryRepository summary = new EventSummaryRepository();
                var events = summary.EventTotalsBySolution(SolutionID);
                var data = (from ev in events
                            select new
                            {
                                SolutionID = ev.SolutionID,
                                Minutes = ev.Minutes,
                                Seconds = ev.Seconds,
                                Hours = ev.Hours,
                                Name = ev.Name
                            }).OrderBy(x => x.Minutes).ToList();
                
                //Bind Summary Tab
                grdSolutionSummary.DataSource = null;
                int? totalSeconds = data.Sum(x => x.Seconds);
                TimeSpan timeSpan = new TimeSpan(0, 0, 0, totalSeconds.GetValueOrDefault());
                lblTotalSolutionTime.Text = string.Format("{0} Days {1} Hours {2} Minutes", timeSpan.Days, timeSpan.Hours, timeSpan.Minutes);
                grdSolutionSummary.DataSource = (from ev in data
                                                 select new
                                                     {
                                                         Name = ev.Name,
                                                         Minutes = ev.Minutes
                                                     }).OrderByDescending(x => x.Minutes).ToList();

                //Bind Graph
                chart1.Series["Series1"].Points.DataBind(data, "Name", "Minutes", "Tooltip=Minutes, Label=Name");
                chart1.ChartAreas["ChartArea1"].CursorX.IsUserEnabled = true;
                chart1.ChartAreas["ChartArea1"].CursorX.IsUserSelectionEnabled = true;
                chart1.ChartAreas["ChartArea1"].AxisX.ScaleView.Zoomable = true;
                chart1.ChartAreas["ChartArea1"].AxisY.ScrollBar.Size = 10;
                chart1.ChartAreas["ChartArea1"].AxisY.ScrollBar.IsPositionedInside = true;
                chart1.ChartAreas["ChartArea1"].AxisX.ScrollBar.Size = 10;
                chart1.ChartAreas["ChartArea1"].AxisX.ScrollBar.IsPositionedInside = true;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        public void LoadSolution(int SolutionID)
        {
            _solutionId = SolutionID;
            BindData(_solutionId);
        }

        private void grdSolutionSummary_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }

        private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            BindData(_solutionId);
        }

      

    }
}
